﻿using Collections.Pooled;
using furion.blog.Domain.Shared;
using furion.blog.Utils;
using Furion.JsonSerialization;
using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using WxRequestLogModel = furion.blog.Domain.Mongo.WxRequestLog;

namespace furion.blog.Domain.Query.WxRequestLog
{
    public class WxRequestLogListQuery : MongoBaseQuery
    {
        public string SearchContent { get; set; } = string.Empty;
        public string Date { get; set; } = string.Empty;//格式：YYYY-MM-DD
        public string Api { get; set; } = string.Empty;//接口
        public string RequestMethod { get; set; } = string.Empty;//请求方式

      
        public FilterDefinition<WxRequestLogModel> Filter()
        {
            FilterDefinitionBuilder<WxRequestLogModel> builder = Builders<WxRequestLogModel>.Filter;
            List<FilterDefinition<WxRequestLogModel>> queryParams = new List<FilterDefinition<WxRequestLogModel>>();
            if (!string.IsNullOrWhiteSpace(Date))
            {
               queryParams.Add( builder.Gte(t=>t.CreatedTime, DateTime.Parse(Date + " 00:00:00")));
               queryParams.Add( builder.Lte(t=>t.CreatedTime, DateTime.Parse(Date + " 23:59:59")));
            }
            if (!string.IsNullOrWhiteSpace(Api)) {
                queryParams.Add(builder.Eq(t=>t.ApiName,Api));
            }
            if (!string.IsNullOrWhiteSpace(RequestMethod))
            {
                queryParams.Add(builder.Eq(t => t.RequestMethod, RequestMethod));
            }
            if (!string.IsNullOrWhiteSpace(SearchContent))
            {
                queryParams.Add(builder.Where(t => (t.ApiName.Contains(SearchContent) || t.Url.Contains(SearchContent))));
            }
            return builder.And(queryParams);
        }


    }
}
